<?php
	require_once('config.php');
	
	if($_GET['key'] <> $NAMIKO_KEY){
		exit('BAD_AUTH');		
	}
	switch($_GET['action']){
		case('init'):
			echo "INT Namiko\t\tSERVER_TIMESTAMP=".mktime();
		break;
		case('fetch'):
			if(isset($_GET['from'])){
				$from = (int) $_GET['from'];
				$query = "SELECT `id`,`ip`,UNIX_TIMESTAMP(`time`) as `time`,`format`,`sw`,`content` FROM `comment` WHERE `id` > $from AND UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`time`) < 10";
				$result = $db->query($query);
				echo 'DATA COUNT='.$result->num_rows."\r\n";
				while($row = $result->fetch_assoc()){
					$id = $row['id'];
					$time = $row['time'];
					$ip = $row['ip'];
					$format = $row['format'];
					$len = strlen($row['content']);
					$content = $row['content'];
					$sw = $row['sw'];
					echo "DATA VER=2\t\tKEY=$NAMIKO_KEY\t\tTIME=$time\t\tIP=$ip\t\tFORMAT=$format\t\tLEN=$len\t\tTEXT=$content\t\tID=$id\t\tSW=$sw\t\t\r\n";
				}
				$db->query("UPDATE `comment` SET `sent` = 1 WHERE `id` > $from AND UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`time`) < 10");
				$db->close();
				file_put_contents('DesktopTouch.log',mktime().'|'.$_GET['totalc']);
			}else{
				echo 'IOP';
			}
		break;
		default:
			echo 'NOP';
		break;
	}
?>